package com.xcc.shopanim;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xcc.shopanim.mode.BannerMode;
import com.xcc.shopanim.mode.NewMainVideoMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MiniLivePhotoFragment extends BaseFragment {
    @BindView(R.id.imgPhoto)
    ImageView imgPhoto;

    public static MiniLivePhotoFragment newInstance(NewMainVideoMode videoMode) {
        BannerMode bannerMode = videoMode.bannerMode;

        Bundle bundle = new Bundle();
        if (bannerMode != null) bundle.putString("bannerMode", new Gson().toJson(bannerMode));
        MiniLivePhotoFragment fragment = new MiniLivePhotoFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) return;
        String json = bundle.getString("bannerMode");
        if (TextUtils.isEmpty(json)) return;
        bannerMode = new Gson().fromJson(json, BannerMode.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup viewGroup, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_minilive_photo, null);
        ButterKnife.bind(this, view);
        return view;
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        isInit = true;
        stateChange();
        if (bannerMode == null) return;
        Glide.with(getActivity())
                .load(bannerMode.getImageUrl())
                .into(imgPhoto);
    }

    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        stateChange();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        stateChange();
    }

    @Override
    public void onResume() {
        super.onResume();
        stateChange();
    }

    @Override
    public void onPause() {
        super.onPause();
        stateChange();
    }

    private void stateChange() {
        boolean isShow;
        if (isPause || hidden) isShow = false;
        else isShow = isVisibleToUser && isInit;
        if (isShow) {
            if (isViewResume) return;
            viewResume();
        } else {
            if (isViewResume) viewPause();
        }
    }

    private boolean isViewResume;
    private boolean isVisibleToUser;
    private boolean isInit;

    private BannerMode bannerMode;

    private void viewResume() {
        isViewResume = true;
    }

    private void viewPause() {
        isViewResume = false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick(R.id.layoutFrame)
    public void onOpenClicked() {
        if (bannerMode == null) return;
        bannerMode.onClick(getActivity());
    }
}
